local BaseData = require("data.base.BaseData")
local EquipmentData = class("EquipmentData", BaseData)
local EquipmentUnitData = require("data.EquipmentUnitData")
local StoreConst = require("const.StoreConst")

local schema = {}
EquipmentData.schema = schema

function EquipmentData:ctor()
    EquipmentData.super.ctor(self)
end

function EquipmentData:init()
    self:_initData()
end

function EquipmentData:save()
    local result = {}
    for _, unit in pairs(self._equipments) do
        local param = unit:getPureData()
        table.insert(result, param)
    end
    G_StoreManager:setGlobalId(StoreConst.EQUIP_GLOBALID, self._globalEquipId)
    G_StoreManager:setDataList(StoreConst.EQUIPMENT, result)
end

function EquipmentData:createTemplateData()
    local data = {}
    data.id = 1
    data.base_id = 2
    data.level = 1
    data.exp = 0
    local unit = EquipmentUnitData.new()
    unit:updateData(data)
    return unit
end

function EquipmentData:_setUnitData(data)
    local unit = self._equipments["k_" .. data.id]
    if not unit then
        unit = EquipmentUnitData.new()
        self._equipments["k_" .. data.id] = unit
    end
    unit:updateData(data)
end

function EquipmentData:getUnitById(id)
    return self._equipments["k_" .. id]
end

function EquipmentData:_initData()
    self._equipments = {}
    self._globalEquipId = G_StoreManager:getGlobalId(StoreConst.EQUIP_GLOBALID)
    local equipments = G_StoreManager:getDataList(StoreConst.EQUIPMENT)
    for i = 1, #equipments do
        self:_setUnitData(equipments[i])
    end
end

function EquipmentData:insertData(datas)
    for i = 1, #datas do
        self:_setUnitData(datas[i])
    end
end

function EquipmentData:updateData(datas)
    for i = 1, #datas do
        self:_setUnitData(datas[i])
    end
end

function EquipmentData:deleteData(ids)
    for i = 1, #ids do
        self._equipments["k_" .. ids[i]] = nil
    end
end

function EquipmentData:addData(base_id, level)
    local data = {}
    data.id = self._globalEquipId
    data.base_id = base_id
    data.level = level or 1
    self._globalEquipId = self._globalEquipId + 1
    self:_setUnitData(data)
end

function EquipmentData:getEquipmentList(filterId)
    local result = {}
    for _, unit in pairs(self._equipments) do
        if unit:getId() ~= filterId then
            table.insert(result, unit)
        end
    end
    return result
end

function EquipmentData:upgradeEquipment(id, level)
    local unit = self:getUnitById(id)
    local curLevel = unit:getLevel()
    local limitLv = unit:getConfig().lv_max
    if curLevel >= limitLv then
        return G_ErrorCode.ERROR_LEVEL_MAX
    end
    local TypeHelper = require("utils.TypeHelper")
    local DataConst = require("const.DataConst")
    local UserDataHelper = require("data.helper.UserDataHelper")
    local EquipDataHelper = require("data.helper.EquipDataHelper")
    local costMoney, costItem = EquipDataHelper.getEquipmentUpCost(unit, level)
    local haveMoney = UserDataHelper.getNumByTypeValue(TypeHelper.TYPE_RESOURCE, DataConst.RES_MONEY)
    local haveItem = UserDataHelper.getNumByTypeValue(costItem.type, costItem.value)
    if haveMoney < costMoney or haveItem < costItem.size then
        return G_ErrorCode.ERROR_COST_NOT
    end

    G_UserData:getResource():useRes(DataConst.RES_MONEY, costMoney)

    G_UserData:getItems():useItem(costItem.value, costItem.size)

    local newLevel = curLevel + level
    if newLevel > limitLv then
        newLevel = limitLv
    end
    unit:setLevel(newLevel)
    G_SignalManager:dispatch(SignalConst.EVENT_EQUIPMENT_UP_SUCCESS)
    return G_ErrorCode.OK
end

function EquipmentData:equipEquipmentToHero(equipId, heroId)
    if equipId > 0 and heroId > 0 then
        local heroUnit = G_UserData:getHero():getUnitById(heroId)
        local unit = self._equipments["k_" .. equipId]
        local oldEquipId = heroUnit:getEquipmentId()
        if oldEquipId > 0 then
            local oldUnit = self._equipments["k_" .. oldEquipId]
            oldUnit:setHeroId(0)
        end
        local oldHeroId = unit:getHeroId()
        if oldHeroId > 0 then
            local oldHeroUnit = G_UserData:getHero():getUnitById(oldHeroId)
            oldHeroUnit:setEquipmentId(0)
        end
        heroUnit:setEquipmentId(equipId)
        unit:setHeroId(heroId)
    end
end

function EquipmentData:clearEquipmentInHero(equipId)
    if equipId > 0 then
        local unit = self._equipments["k_" .. equipId]
        local heroId = unit:getHeroId()
        local heroUnit = G_UserData:getHero():getUnitById(heroId)
        if heroUnit then
            heroUnit:setEquipmentId(0)
        end
        unit:setHeroId(0)
    end
end

return EquipmentData
